<#assign pkCount = 0 />
<#list table.columnList as column>
	<#if column.primaryKey>
		<#assign pkCount = pkCount + 1>
		<#if pkCount = 1>
			<#assign firstPkColumn = column>
		</#if>
	</#if>
</#list>
package ${package};

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.sosostudio.dbking.DbKing;
import org.sosostudio.dbking.Row;
import org.sosostudio.dbking.RowList;
import org.sosostudio.dbking.autocode.PageList;
import org.sosostudio.dbking.oom.WhereClause;
import org.sosostudio.dbking.oom.DeleteSql;
import org.sosostudio.dbking.oom.ExtraClause;
import org.sosostudio.dbking.oom.InsertKeyValueClause;
import org.sosostudio.dbking.oom.InsertSql;
import org.sosostudio.dbking.oom.LogicalOp;
import org.sosostudio.dbking.oom.OrderByClause;
import org.sosostudio.dbking.oom.RelationOp;
import org.sosostudio.dbking.oom.SelectSql;
import org.sosostudio.dbking.oom.SetOp;
import org.sosostudio.dbking.oom.UpdateKeyValueClause;
import org.sosostudio.dbking.oom.UpdateSql;

/**
 * ${table.name}
 *
 * @author generated by dbking autocode
 */
public class ${table.definationName}Dao {

	private DbKing dbKing;

	public ${table.definationName}Dao() {
		dbKing = new DbKing();
	}
	
	public ${table.definationName}Dao(DbKing dbKing) {
		this.dbKing = dbKing;
	}

	<#if table.table>
	public int add(${table.definationName} ${table.variableName}) {
		${table.variableName}.truncateString();
		InsertSql insertSql = new InsertSql().setTableName(${table.definationName}.${table.name})
			.setInsertKeyValueClause(
				new InsertKeyValueClause()
		<#list table.columnList as column>
					.add${column.type.getName()}Clause(${table.definationName}.${column.name}, ${table.variableName}.get${column.definationName}())
		</#list>
		);
		return dbKing.execute(insertSql);
	}

	<#if pkCount &gt; 0>
	public int update(${table.definationName} ${table.variableName}) {
		${table.variableName}.truncateString();
		UpdateSql updateSql = new UpdateSql().setTableName(${table.definationName}.${table.name})
			.setUpdateKeyValueClause(
				new UpdateKeyValueClause()
		<#list table.columnList as column>
					.add${column.type.getName()}Clause(${table.definationName}.${column.name}, ${table.variableName}.get${column.definationName}())
		</#list>
			).setWhereClause(
				new WhereClause(LogicalOp.AND)
		<#list table.columnList as column>
			<#if column.primaryKey>
				.add${column.type.getName()}Clause(${table.definationName}.${column.name}, RelationOp.EQUAL, ${table.variableName}.get${column.definationName}())
			</#if>
		</#list>	
			);
		return dbKing.execute(updateSql);
	}
	</#if>

	public int update(UpdateKeyValueClause updateKeyValueClause, WhereClause whereClause) {
		UpdateSql updateSql = new UpdateSql().setTableName(${table.definationName}.${table.name})
			.setUpdateKeyValueClause(updateKeyValueClause)
			.setWhereClause(whereClause);
		return dbKing.execute(updateSql);
	}
	
	public int delete(WhereClause whereClause) {
		DeleteSql deleteSql = new DeleteSql().setTableName(${table.definationName}.${table.name})
			.setWhereClause(whereClause);
		return dbKing.execute(deleteSql);
	}
	
	<#if pkCount = 1>
	public int delete(${firstPkColumn.type.getName()} ${firstPkColumn.variableName}) {
		DeleteSql deleteSql = new DeleteSql().setTableName(${table.definationName}.${table.name})
			.setWhereClause(new WhereClause(LogicalOp.AND).add${firstPkColumn.type.getName()}Clause(${table.definationName}.${firstPkColumn.name}, RelationOp.EQUAL, ${firstPkColumn.variableName}));
		return dbKing.execute(deleteSql);
	}
	
	public int delete(Collection<${firstPkColumn.type.getName()}> ${firstPkColumn.variableName}Collection) {
		DeleteSql deleteSql = new DeleteSql().setTableName(${table.definationName}.${table.name})
			.setWhereClause(new WhereClause(LogicalOp.AND).add${firstPkColumn.type.getName()}Clause(${table.definationName}.${firstPkColumn.name}, SetOp.IN, ${firstPkColumn.variableName}Collection));
		return dbKing.execute(deleteSql);
	}
	
	public ${table.definationName} get(${firstPkColumn.type.getName()} ${firstPkColumn.variableName}) {
		WhereClause whereClause = new WhereClause(LogicalOp.AND).add${firstPkColumn.type.getName()}Clause(${table.definationName}.${firstPkColumn.name}, RelationOp.EQUAL, ${firstPkColumn.variableName});
		List<${table.definationName}> list = query(whereClause, null, null);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	</#if>
	</#if>

	public PageList<${table.definationName}> query(WhereClause whereClause, ExtraClause extraClause, OrderByClause orderByClause, int pageSize, int pageNumber) {
		SelectSql selectSql = new SelectSql().setTableName(${table.definationName}.${table.name})
			.setColumns("*")
			.setWhereClause(whereClause)
			.setExtraClause(extraClause)
			.setOrderByClause(orderByClause);
		RowList rowList = dbKing.query(selectSql, pageSize, pageNumber);
		PageList<${table.definationName}> pageList = new PageList<${table.definationName}>(rowList.getPageSize(), rowList.getPageNumber(), rowList.getTotalRowCount());
		for (Row row : rowList) {
			${table.definationName} ${table.variableName} = new ${table.definationName}();
			<#list table.columnList as column>
			${table.variableName}.set${column.definationName}(row.get${column.type.getName()}(${table.definationName}.${column.name}));
			</#list>
			pageList.add(${table.variableName});
		}
		return pageList;
	}

	public List<${table.definationName}> query(WhereClause whereClause, ExtraClause extraClause, OrderByClause orderByClause) {
		SelectSql selectSql = new SelectSql().setTableName(${table.definationName}.${table.name})
			.setColumns("*")
			.setWhereClause(whereClause)
			.setExtraClause(extraClause)
			.setOrderByClause(orderByClause);
		RowList rowList = dbKing.query(selectSql);
		List<${table.definationName}> list = new ArrayList<${table.definationName}>();
		for (Row row : rowList) {
			${table.definationName} ${table.variableName} = new ${table.definationName}();
			<#list table.columnList as column>
			${table.variableName}.set${column.definationName}(row.get${column.type.getName()}(${table.definationName}.${column.name}));
			</#list>
			list.add(${table.variableName});
		}
		return list;
	}

}